package demo.hdz.mycustomview.customview;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import demo.hdz.mycustomview.R;
import demo.hdz.mycustomview.utils.Logger;

/**
 * @author hdz
 * @date 2018/11/30 18:54
 */
public class CustomImageTextView extends LinearLayout {

    private static final String TAG = "CustomImageTextView";

    private ImageView mImageView;
    private TextView mTextView;

    private int mImageId = -1;
    private int mImageIdPress = -1;
    private int mImageWidth = LayoutParams.WRAP_CONTENT;
    private int mImageHeight = LayoutParams.WRAP_CONTENT;
    private String mText = "";
    private int mTextId = -1;
    private int mTextSize = 0;
    private int mTextColor = 0;
    private int mTextColorPress = 0;
    private Bitmap mImageBitmap = null;
    private Bitmap mImageBitmapPress = null;


    /**
     * 图片与文本之间的间距
     */
    private int mInnerPadding;
    /**
     *  文本相对图片的位置：0底部，1右边，2顶部，3左边
     */
    private int mTextPosition = 0;
    /**
     * 是否有文本
     */
    private boolean mHaveText = true;
    private int mImageAlpha = 0;


    public CustomImageTextView(Context context) {
        this(context, null);
    }

    public CustomImageTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomImageTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrValues(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomImageTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        this(context, attrs, defStyleAttr);
    }


    @Override
    public boolean performClick() {
        return super.performClick();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                down();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                performClick();
                up();
                break;
            default:
                up();
                return super.onTouchEvent(event);
        }
        return true;
    }

    private void getAttrValues(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        if (attrs == null) {
            return;
        }
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.imageText, defStyleAttr, 0);

        // 获取文本内容
        mTextId = typedArray.getResourceId(R.styleable.imageText_text, -1);
        if (mTextId == -1) {
            mText = typedArray.getString(R.styleable.imageText_text);
        }
        // 文字大小
        mTextSize = typedArray.getDimensionPixelSize(R.styleable.imageText_textSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 6, getResources().getDisplayMetrics()));
        // 文字颜色
        int textColorId = typedArray.getResourceId(R.styleable.imageText_textColor, -1);
        if (textColorId == -1) {
            mTextColor = typedArray.getColor(R.styleable.imageText_textColor, 0);
        } else {
            mTextColor = getResources().getColor(textColorId);
        }
        // 按压时的文字颜色
        int textColorPressId = typedArray.getResourceId(R.styleable.imageText_pressTextColor, -1);
        if (textColorPressId == -1) {
            mTextColorPress = typedArray.getColor(R.styleable.imageText_pressTextColor, 0);
        } else {
            mTextColorPress = getResources().getColor(textColorPressId);
        }

        // 获取图片宽高度
        mImageWidth = typedArray.getDimensionPixelSize(R.styleable.imageText_imageWidth, LayoutParams.WRAP_CONTENT);
        mImageHeight = typedArray.getDimensionPixelSize(R.styleable.imageText_imageHeight, LayoutParams.WRAP_CONTENT);
        Logger.d(TAG+",getAttrValues: mImageWidth="+mImageWidth+", mImageHeight="+mImageHeight);

        // 获取默认图片ID和点击时的图片ID
        mImageId = typedArray.getResourceId(R.styleable.imageText_image, -1);
        mImageIdPress = typedArray.getResourceId(R.styleable.imageText_pressImage, -1);

        // 文本位置：0底部，1右边，2顶部，3左边
        mTextPosition = typedArray.getInt(R.styleable.imageText_textPosition, 0);
        // 是否有文字
        mHaveText = typedArray.getBoolean(R.styleable.imageText_haveText, true);

        // 图片与文字之间的间距
        mInnerPadding = typedArray.getDimensionPixelSize(R.styleable.imageText_innerPadding, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 10, getResources().getDisplayMetrics()));

        // 使用完后记得回收
        typedArray.recycle();
    }

    private void init(Context context) {
        this.setGravity(Gravity.CENTER);
        if (mImageView == null) {
            mImageView = new ImageView(context);
        }
        addView(mImageView);

        if (mHaveText) {
            if (mTextView == null) {
                mTextView = new TextView(context);
            }
            switch (mTextPosition) {
                case 0:
                    this.setOrientation(LinearLayout.VERTICAL);
                    addView(mTextView);
                    break;
                case 1:
                    this.setOrientation(LinearLayout.HORIZONTAL);
                    addView(mTextView);
                    break;
                case 2:
                    this.setOrientation(LinearLayout.VERTICAL);
                    addView(mTextView, 0);
                    break;
                case 3:
                    this.setOrientation(LinearLayout.HORIZONTAL);
                    addView(mTextView, 0);
                    break;
                default:
                    break;
            }

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.gravity = Gravity.CENTER_HORIZONTAL;
            mTextView.setLayoutParams(lp);

            this.setInnerPadding(mInnerPadding);
            if (mTextId == -1) {
                this.setText(mText);
            } else {
                this.setText(mTextId);
            }
            this.setTextColor(mTextColor);
            this.setTextSize(mTextSize);
        }
        if (mImageId != -1) {
            this.setImageResource(mImageId);
        }
        if (mImageIdPress != -1) {
            this.setImageResourcePress(mImageIdPress);
        }
        this.setImageSize(mImageWidth, mImageHeight);
    }


    private void down() {
        if (mHaveText) {
            this.setTextColor(mTextColorPress);
        }
        if (mImageIdPress != -1) {
            this.setImageResource(mImageIdPress);
        } else if (mImageBitmapPress != null) {
            this.setImageBitmap(mImageBitmapPress);
        } else {
            mImageAlpha = mImageView.getImageAlpha();
            mImageView.setAlpha(200);
        }
    }
    private void up() {
        if (mHaveText) {
            this.setTextColor(mTextColor);
        }
        if (mImageIdPress != -1) {
            this.setImageResource(mImageId);
        } else if (mImageBitmap != null) {
            this.setImageBitmap(mImageBitmap);
        } else {
            mImageView.setAlpha(mImageAlpha);
        }
    }

    /**
     * 设置默认的图片。
     *
     * @param resourceID 图片ID
     */
    private void setImageResource(int resourceID) {
        if (mImageId == -1) {
            mImageId = resourceID;
        }
        this.mImageView.setImageResource(resourceID);
    }


    /**
     * 设置按下的图片
     *
     * @param resourceID 图片id
     */
    public void setImageResourcePress(int resourceID) {
        mImageIdPress = resourceID;
    }



    /**
     * 设置显示的文字
     *
     * @param resourceID 字符串ID
     */
    public void setText(int resourceID) {
        this.mTextView.setText(resourceID);
    }


    /**
     * 设置显示的文字
     *
     * @param text 字符串
     */
    public void setText(String text) {
        this.mTextView.setText(text);
    }


    /**
     * 设置文本的颜色
     *
     * @param color 颜色值
     */
    public void setTextColor(int color) {
        if (mTextColor == 0) {
            mTextColor = color;
        }
        this.mTextView.setTextColor(color);
    }


    /**
     * 设置按下文本的颜色
     *
     * @param color 颜色值
     */
    public void setTextColorPress(int color) {
        mTextColorPress = color;
    }


    /**
     * 设置字体大小
     *
     * @param size 文本字体大小
     */
    public void setTextSize(float size) {
        this.mTextView.setTextSize(size);
    }

    /**
     *  设置图片尺寸
     *
     * @param width 图片宽度
     * @param height 图片高度
     */
    public void setImageSize(int width, int height) {
        mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        mImageView.setLayoutParams(new LayoutParams(width, height));
    }


    /**
     * 设置图片与文字之间的间隔
     * *
     * @param padding 图片与文字之间的间隔
     */
    public void setInnerPadding(int padding) {
        switch (mTextPosition) {
            case 1:
                this.mTextView.setPadding(padding, 0, 0, 0);
                break;
            case 2:
                this.mTextView.setPadding(0, 0, 0, padding);
                break;
            case 3:
                this.mTextView.setPadding(0, 0, padding, 0);
                break;
            default:
                this.mTextView.setPadding(0, padding, 0, 0);
                break;
        }
    }

    public void setImageBitmap(Bitmap bitmap) {
        if (mImageBitmap == null) {
            mImageBitmap = bitmap;
        }
        mImageView.setImageBitmap(bitmap);
    }

    public void setImageBitmapPress(Bitmap bitmap) {
        mImageBitmapPress = bitmap;
    }
}
